﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AjaxControlFramework
{
    public abstract class ScriptReference
    {
        //------// Properties \\--------------------------------------------\\
        public virtual bool Rendered
        {
            get
            {
                return InternalScriptManager.Instance.RenderedScripts.ContainsKey(Name);
            }
            set
            {
                if (value)
                {
                    if (!InternalScriptManager.Instance.RenderedScripts.ContainsKey(Name))
                    {
                        InternalScriptManager.Instance.RenderedScripts.Add(Name, this);
                    }
                }
                else
                {
                    if (InternalScriptManager.Instance.RenderedScripts.ContainsKey(Name))
                    {
                        InternalScriptManager.Instance.RenderedScripts.Remove(Name);
                    }
                }
            }
        }


        private string _name = String.Empty;
        public virtual string Name
        {
            get { return _name; }
            set
            {
                if (value == null)
                {
                    _name = String.Empty;
                }
                else
                {
                    _name = value;
                }
            }
        }


        private bool _enabled = true;
        public virtual bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }


        private List<string> _dependencies = new List<string>();
        public virtual List<string> Dependencies
        {
            get { return _dependencies; }
            set { _dependencies = value; }
        }


        private bool _controlCreateScript = false;
        public virtual bool ControlCreateScript
        {
            get { return _controlCreateScript; }
            set { _controlCreateScript = value; }
        }


        private Type _createScriptControlType = null;
        public virtual Type CreateScriptControlType
        {
            get { return _createScriptControlType; }
            set { _createScriptControlType = value; }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        protected ScriptReference(string name)
        {
            Name = name;
        }


        protected ScriptReference(string name, bool enabled)
        {
            Name = name;
            Enabled = enabled;
        }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        public abstract string GenerateScriptBlock();


        public virtual void AddDependency(string scriptName)
        {
            if (!Dependencies.Contains(scriptName))
            {
                Dependencies.Add(scriptName);
            }
        }
        //------\\ Methods //-----------------------------------------------//
    }
}
